package com.liucanwen.citylist.hccitylist;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import com.liucanwen.citylist.R;
import com.liucanwen.citylist.hccitylist.adapter.SerchAdapter;
import com.liucanwen.citylist.hccitylist.cityinterface.City;

import java.util.ArrayList;

/**
 * Created by hechao on 2017/7/19.
 */
public class SelectedCityActivity extends Activity implements View.OnClickListener, TextWatcher {

    private ListView mLv_search;
    private SerchAdapter mSearchAdapter;
    private EditText searchBox ;
    private ImageView img_cancel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_selected);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        //-----------------------ET
        searchBox = (EditText) findViewById(R.id.input_search_query);
        searchBox.setFocusable(true);
        searchBox.addTextChangedListener(this);
        img_cancel = (ImageView) findViewById(R.id.img_cancel);
        img_cancel.setOnClickListener(this);
        mLv_search = (ListView) findViewById(R.id.lv_search);
        mLv_search.setAdapter(mSearchAdapter = new SerchAdapter(this));
        mLv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String city = filterList.get(position);
                if (city.length() != 1){
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("city",city);
                    intent.putExtras(bundle);
                    SelectedCityActivity.this.setResult(911,intent);
                    finish();
                }
            }
        });
        filterList = new ArrayList<String>();

    }

    @Override
    public void onClick(View v) {
        if ( v.getId() == R.id.img_cancel ){
            searchBox.setText("");
            img_cancel.setVisibility(View.INVISIBLE);
        }else{
            finish();
        }
    }
    //-----------------
    private void afterSelected(String city) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("city", city);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent，当然也可以自己定义新的Bundle
        finish();
    }

    //----------------------------------ET
    private SearchListTask curSearchTask = null;
    private ArrayList<String> filterList;
    boolean inSearchMode = false;
    private Object searchLock = new Object();

    private String searchString;
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        searchString = searchBox.getText().toString().trim().toUpperCase();

        if (curSearchTask != null
                && curSearchTask.getStatus() != AsyncTask.Status.FINISHED) {
            try {
                curSearchTask.cancel(true);
            } catch (Exception e) {
                Log.i("ChooseCity_Tag", "Fail to cancel running search task");
            }

        }
        curSearchTask = new SearchListTask();
        curSearchTask.execute(searchString);
        if ( !img_cancel.isShown() )
            img_cancel.setVisibility(View.VISIBLE);
    }

    private class SearchListTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            filterList.clear();

            String keyword = params[0];

            inSearchMode = (keyword.length() > 0);

            if (inSearchMode) {
                // get all the items matching this
                for (String city : City.getInstance().getCityList()) {

                    boolean isPinyin = city.toUpperCase()
                            .indexOf(keyword) > -1;
                    boolean isChinese = city.indexOf(keyword) > -1;

                    if (isPinyin || isChinese) {
                        filterList.add(city);
                    }

                }

            }
            return null;
        }

        protected void onPostExecute(String result) {

            synchronized (searchLock) {

                if (inSearchMode) {
                    mSearchAdapter.setData(filterList);
                    mSearchAdapter.notifyDataSetChanged();
                    mLv_search.setVisibility(View.VISIBLE);
//                    mCityLv.setVisibility(View.INVISIBLE);
                } else {
                    mLv_search.setVisibility(View.INVISIBLE);
//                    mCityLv.setVisibility(View.VISIBLE);
//                    mCityLv.setAdapter(mCityAdapter);
                }
//                hideshowHeadView();
            }
        }
    }
}
